package com.hmjk.health;

import com.hmjk.health.home.HomeActivity;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.text.Html;
import android.view.KeyEvent;
import android.view.View;

import android.view.Window;

import android.view.animation.DecelerateInterpolator;

import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SplashActivity extends Activity {
	private HealthApp mApp = null;
	private ImageView rqq = null;
	private View rqq_range = null;
	private final int MSG_SHAKE_START = 100;
	private final int MSG_ENTER_HOME = 101;
	private boolean shaking = false;
	private String trans_from = null;
	private String trans_type = null;
	private String trans_param = null;
	private boolean enter = false;
	private AnimatorSet mAnimSet = null;
	//private boolean isGuided = true;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mApp = (HealthApp)getApplication();
		setContentView(R.layout.splashscreen);
		rqq_range = findViewById(R.id.rqq_range);
		rqq = (ImageView) findViewById(R.id.rqq);
		rqq.setVisibility(View.INVISIBLE);
		
		TextView skip = (TextView) findViewById(R.id.skip);
		String skipstr = getResources().getString(R.string.splash_skip);
		skip.setText(Html.fromHtml(skipstr));
		skip.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(mAnimSet != null){
					mAnimSet.cancel();
				}
				enterActivity();
			}
		});
		
		shaking = false;
		
		boolean downloadAD = false;
		if(((int)(Math.random()*3) == 0) || (mApp.getADnumber() == 0)){
			downloadAD = true;
		}
		mApp.ADDebug("SplashActivity onCreate");
		mApp.loadADInfo(downloadAD);
		
		if(downloadAD){
			mApp.getDataManager().downloadAD();
		}
		
		mApp.checkNewVersion();
		
		Intent i = getIntent();
		if(i.hasExtra("from") && i.hasExtra("type") && i.hasExtra("param")){
			trans_from = i.getStringExtra("from");
			trans_type = i.getStringExtra("type");
			trans_param = i.getStringExtra("param");
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
	   	
	   	if(keyCode == KeyEvent.KEYCODE_BACK){
	   		return true;
	   		
	   	}
	   	return super.onKeyDown(keyCode,event);
    }
	
	private void enterActivity(){
		if(enter)
			return;
		
		enter = true;
		String last = mApp.getLastVisit();
		
		if(last.equals(IndexActivity.ENTER_LOCATION)){
			Intent i = new Intent(this,HomeActivity.class);
			i.putExtra("enter_type", IndexActivity.ENTER_LOCATION);
			if((trans_from != null) && (trans_type != null) && (trans_param != null)){
				i.putExtra("from", trans_from);
				i.putExtra("type", trans_type);
				i.putExtra("param", trans_param);
			}
			
			startActivity(i);
			overridePendingTransition(R.anim.fadeinslow, R.anim.fadeoutslow);
			finish();
			return;
		}
			
		if(last.equals(IndexActivity.ENTER_HEALTH)){
			Intent i = new Intent(this,HomeActivity.class);
			i.putExtra("enter_type", IndexActivity.ENTER_HEALTH);
			if((trans_from != null) && (trans_type != null) && (trans_param != null)){
				i.putExtra("from", trans_from);
				i.putExtra("type", trans_type);
				i.putExtra("param", trans_param);
			}
			
			startActivity(i);
			overridePendingTransition(R.anim.fadeinslow, R.anim.fadeoutslow);
			finish();
			return;
		}
		
		if(last.equals(IndexActivity.ENTER_STEPCOUNTER)){
			//TODO:enter step counter activity
			overridePendingTransition(R.anim.fadeinslow, R.anim.fadeoutslow);
			finish();
			return;
		}
		
		Intent i = new Intent(this,IndexActivity.class);
		
		startActivity(i);
		overridePendingTransition(R.anim.fadeinslow, R.anim.fadeoutslow);
		finish();
	}
	
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			switch(msg.what){
			case MSG_ENTER_HOME:{
				enterActivity();
			}
			break;
			
			case MSG_SHAKE_START:{
				//float density = getResources().getDisplayMetrics().density;
				//float rightGap = density * 20;
				// bottomGap = density * 40;
				
				int rw = rqq_range.getMeasuredWidth();
				int rh = rqq_range.getMeasuredHeight();
				
				int qw = rqq.getMeasuredWidth();
				int qh = rqq.getMeasuredHeight();
				
				float x = rqq.getTranslationX();
				float y = rqq.getTranslationY();
				
				PointF start = new PointF(x - qw/2,y -  qh/2);
				PointF end = new PointF(x + (rw-qw),y+(rh-qh));
				
				
				ValueAnimator rqqfly = ValueAnimator.ofObject(new FlyEvaluator(), start, end);
				rqqfly.addUpdateListener(new AnimatorUpdateListener(){

					@Override
					public void onAnimationUpdate(ValueAnimator anim) {
						PointF p = (PointF) anim.getAnimatedValue();
						rqq.setTranslationX(p.x);
						rqq.setTranslationY(p.y);
					}
					
				});
				
				rqqfly.setInterpolator(new DecelerateInterpolator());
				
				ObjectAnimator scaleY = ObjectAnimator.ofFloat(rqq, "scaleY", 0.2f, 1.2f, 1.0f);
				ObjectAnimator scaleX = ObjectAnimator.ofFloat(rqq, "scaleX", 0.2f, 1.2f, 1.0f);
				
				mAnimSet = new AnimatorSet();
				mAnimSet.play(rqqfly).with(scaleX).with(scaleY);
				mAnimSet.setDuration(7 * 1000);
				mAnimSet.addListener(new AnimatorListener(){

					@Override
					public void onAnimationCancel(Animator arg0) {}

					@Override
					public void onAnimationEnd(Animator arg0) {
						mHandler.removeMessages(MSG_ENTER_HOME);
						Message m = mHandler.obtainMessage(MSG_ENTER_HOME);
						mHandler.sendMessageDelayed(m, 1000);
					}

					@Override
					public void onAnimationRepeat(Animator arg0) {}

					@Override
					public void onAnimationStart(Animator arg0) {}
				});
				rqq.setVisibility(View.VISIBLE);
				mAnimSet.start();
				
				shaking = true;				
			}
			break;
			}
		}
	};
	
	class FlyEvaluator implements TypeEvaluator{

		@Override
		public Object evaluate(float in_t, Object arg1, Object arg2) {
			PointF s = (PointF) arg1;
			PointF e = (PointF) arg2;
			PointF m = new PointF(s.x + (e.x - s.x)/2, s.y + (e.y - s.y)/2);
			if(in_t <= 0.5f){
				PointF p0 = s;
				PointF p1 = new PointF(e.x, s.y + (e.y - s.y)/8);
				PointF p2 = m;
			
				float t = in_t * 2;
				float x = (1.0f - t) * (1.0f - t) * p0.x
						+ 2 * t * (1.0f - t) * p1.x
						+ t * t * p2.x;
				
				float y = (1.0f - t) * (1.0f - t) * p0.y
						+ 2 * t * (1.0f - t) * p1.y
						+ t * t * p2.y;
				
				PointF ret = new PointF(x,y);
				return ret;
			}else{
				PointF p0 = m;
				PointF p1 = new PointF(s.x, e.y);
				PointF p2 = e;
				float t = (in_t-0.5f) * 2;
				float x = (1.0f - t) * (1.0f - t) * p0.x
						+ 2 * t * (1.0f - t) * p1.x
						+ t * t * p2.x;
				
				float y = (1.0f - t) * (1.0f - t) * p0.y
						+ 2 * t * (1.0f - t) * p1.y
						+ t * t * p2.y;
				
				PointF ret = new PointF(x,y);
				return ret;
			}
		}
		
	}
	
	@Override
	public void onResume(){
		super.onResume();
		
		if(!shaking){
			Message m = mHandler.obtainMessage(MSG_SHAKE_START);
			mHandler.sendMessageDelayed(m, 500);
		}else{
			mHandler.removeMessages(MSG_ENTER_HOME);
			Message m = mHandler.obtainMessage(MSG_ENTER_HOME);
			mHandler.sendMessageDelayed(m, 3000);
		}
	}

	@Override
	public void onPause(){
		super.onPause();
		mHandler.removeMessages(MSG_ENTER_HOME);
		mHandler.removeMessages(MSG_SHAKE_START);
	}
}
